package com.jxdinfo.hussar.cloud.module.example.tjjc.towernoticeperson.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernoticeperson.dao.TowerNoticePersonMapper;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernoticeperson.model.TowerNoticePerson;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernoticeperson.service.TowerNoticePersonService;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernoticeperson.vo.TowerNoticePersonPageVO;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernoticeperson.vo.TowerNoticePersonSearchVO;
import com.jxdinfo.hussar.cloud.module.example.tjjc.towernoticeperson.vo.TowerNoticePersonVO;
import com.jxdinfo.hussar.cloud.module.example.tjjc.util.DateUtil;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.jxdinfo.hussar.platform.core.utils.BeanUtil;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 塔机通知人员服务实现类
 */
@Service("tjjc.towernoticeperson.TowerNoticePersonServiceImpl")
@HussarDs("master")
public class TowerNoticePersonServiceImpl extends HussarServiceImpl<TowerNoticePersonMapper, TowerNoticePerson> implements TowerNoticePersonService {

    private static final Logger logger = LoggerFactory.getLogger(TowerNoticePersonServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private TowerNoticePersonMapper towerNoticePersonMapper;

    @Override
    public ApiResponse<TowerNoticePersonPageVO> findByPage(TowerNoticePersonSearchVO towerNoticePersonSearchVO) {
        QueryWrapper<TowerNoticePerson> wrapper = new QueryWrapper<>();
        Page<TowerNoticePerson> page = new Page<>();
        page.setPages(towerNoticePersonSearchVO.getPage());
        page.setSize(towerNoticePersonSearchVO.getRows());
        Page<TowerNoticePerson> towerNoticePersonPage = towerNoticePersonMapper.selectPage(page, wrapper);

        TowerNoticePersonPageVO towerNoticePersonPageVO = new TowerNoticePersonPageVO();
        towerNoticePersonPageVO.setCode(RETURN_CODE);
        towerNoticePersonPageVO.setData(towerNoticePersonPage.getRecords());
        towerNoticePersonPageVO.setCount(towerNoticePersonPage.getTotal());

        return ApiResponse.success(towerNoticePersonPageVO);
    }

    @Override
    public ApiResponse<List<TowerNoticePersonVO>> findByList(TowerNoticePersonSearchVO towerNoticePersonSearchVO) {
        QueryWrapper<TowerNoticePerson> wrapper = new QueryWrapper<>();
        List<TowerNoticePerson> towerNoticePeople = towerNoticePersonMapper.selectList(wrapper);
        List<TowerNoticePersonVO> towerNoticePersonVOS = BeanUtil.copyToList(towerNoticePeople, TowerNoticePersonVO.class);
        return ApiResponse.success(towerNoticePersonVOS);
    }

    @Override
    public ApiResponse<String> findNoticePerson(TowerNoticePersonSearchVO towerNoticePersonSearchVO) {
        QueryWrapper<TowerNoticePerson> wrapper = new QueryWrapper<>();

        if(ObjectUtils.isNotEmpty(towerNoticePersonSearchVO.getNoticeId())) {
            wrapper.eq("notice_id", towerNoticePersonSearchVO.getNoticeId());
        }

        List<TowerNoticePerson> list = towerNoticePersonMapper.selectList(wrapper);

        String str = "";

        if (ObjectUtils.isNotEmpty(str)) {
            for (TowerNoticePerson towerNoticePerson : list) {
                if (StringUtils.isNotEmpty(str)) {
                    str = str + "," + towerNoticePerson.getPersonName();
                } else {
                    str = towerNoticePerson.getPersonName();
                }
            }
        }

        return ApiResponse.success(str);
    }

    @Override
    public ApiResponse<String> add(TowerNoticePersonSearchVO towerNoticePersonSearchVO) {
        TowerNoticePerson towerNoticePerson = new TowerNoticePerson();
        BeanUtil.copyProperties(towerNoticePersonSearchVO, towerNoticePerson);
        towerNoticePerson.setTyProjectCode("10101");//todo
        towerNoticePerson.setTyCreateTime(DateUtil.toLocalDateTime(new Date()));
        towerNoticePerson.setTyCreateUserid("ContextUtil.getUserId()");//todo
        towerNoticePerson.setTyCreateUserName("ContextUtil.getUserName()");//todo

        towerNoticePersonMapper.insert(towerNoticePerson);

        return ApiResponse.success();
    }

    @Override
    public ApiResponse<String> updateTowerNoticePerson(TowerNoticePersonSearchVO towerNoticePersonSearchVO) {
        TowerNoticePerson towerNoticePersonOld = towerNoticePersonMapper.selectById(towerNoticePersonSearchVO.getId());

        if(towerNoticePersonOld != null){

            BeanUtil.copyProperties(towerNoticePersonSearchVO, towerNoticePersonOld);
            towerNoticePersonOld.setTyLastUpdateTime(DateUtil.toLocalDateTime(new Date()));
            towerNoticePersonOld.setTyLastUpdateUserid("ContextUtil.getUserId()");//todo
            towerNoticePersonOld.setTyLastUpdateUserName("ContextUtil.getUserName()");//todo

            towerNoticePersonMapper.updateById(towerNoticePersonOld);
        }

        return ApiResponse.success();
    }

    @Override
    public ApiResponse<String> deleteById(TowerNoticePersonSearchVO towerNoticePersonSearchVO) {
        TowerNoticePerson towerNoticePersonOld = towerNoticePersonMapper.selectById(towerNoticePersonSearchVO.getId());

        if(towerNoticePersonOld != null){

            //todo 原代码为逻辑删除
            towerNoticePersonMapper.deleteById(towerNoticePersonOld.getId());
        }

        return ApiResponse.success();
    }
}
